package com.hadoop.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * <p></p>
 *
 * @author Andy
 * @date 2017/9/28
 */
public class JedisPoolUtils {

    private static volatile JedisPool jedisPool = null;//被volatile修饰的变量不会被本地线程缓存，对该变量的读写都是直接操作共享内存。

    private JedisPoolUtils(){}

    public static JedisPool getJedisPoolInstance() {
        if (null == jedisPool) {
            synchronized (JedisPoolUtils.class) {
                if (null == jedisPool) {
                    JedisPoolConfig poolConfig = new JedisPoolConfig();
                    poolConfig.setMaxTotal(1000);
                    poolConfig.setMaxIdle(32);
                    poolConfig.setMaxWaitMillis(100*1000);
                    poolConfig.setTestOnBorrow(true);
                    jedisPool = new JedisPool(poolConfig, "192.168.20.200", 6379);
                }
            }
        }
        return jedisPool;
    }

    /**
     *  用完之后，放回到连接池
     * @param jedis
     */
    public static void release(Jedis jedis) {
        if (null != jedis) {
            jedis.close();
        }
    }
}
